<?php
/**
 * @author trunghpb
 */
class fwiRequest {

    private $_params = array();
    private $_getParams = array();
    private $_postParams = array();
    private $_serverParams = array();    
    private $_method = 'POST';

    public function __construct() {
        $this->setMethod(strtoupper($_SERVER['REQUEST_METHOD']));
        $this->initParam();
    }

    public function setMethod($method) {
        $this->_method = $method;
    }

    public function isPost() {
        return ($this->_method == 'POST');
    }

    public function escapeMagicQuotes(& $data) {
        if (get_magic_quotes_gpc() == 1) {
            if (is_array($data)) {
                foreach ($data as $k => $v) {
                    if (is_array($item)) {
                        $this->processMagicQuotes($v);
                    }
                    else {
                        $data[$k] = stripslashes($v);
                    }
                }
            }
            else {
                $data = stripslashes($data);
            }
        }
    }

    private function escapeArray($ary, $var){
        foreach ($ary as $key => $val) {
            $data = (isset($ary[$key]) && $ary[$key] != "") ? $ary[$key] : $val;
            $this->escapeMagicQuotes($data);
            $this->_params[$key] = $data;
            $varName = "_{$var}Params";
            $this->{$varName}[$key] = $this->_params[$key];
        }
    }

    private function initParam() {
        $this->escapeArray($_GET, 'get');
        $this->escapeArray($_POST, 'post');
        $this->escapeArray($_SERVER, 'server');
    }

    public function getParams() {
        return $this->_params;
    }

    public function getParam($name) {
        return $this->_params[$name];
    }

    public function getPostParams() {
        return $this->_postParams;
    }

    public function getPostParam($name) {
        return $this->_postParams[$name];
    }

    public function getGetParams() {
        return $this->_getParams;
    }

    public function getGetParam($name) {
        return $this->_getParams[$name];
    }

    public function hasParam($name) {
        return isset($this->_params[$name]);
    }

    public function hasPostParam($name) {
        return isset($this->_postParams[$name]);
    }

    public function hasGetParam($name) {
        return isset($this->_getParams[$name]);
    }

    public function getFile($name) {
        return $_FILES[$name]['name'];
    }

    public function getFiles() {
        return $_FILES;
    }

    public function getTempFile($name) {
        return $_FILES[$name]['tmp_name'];
    }

    public function getFileSize($name) {
        return $_FILES[$name]['size'];
    }

    public function getFileType($name) {
        return $_FILES[$name]['type'];
    }

    public function copyFile($name, $dir) {
        if (substr($dir, -1) != '/') {
            $dir .= '/';
        }
        return @copy($_FILES[$name]['tmp_name'], $dir.$_FILES[$name]['name']);
    }

    public function getServer($name){
        return $this->_serverParams[$name];
    }


}
